<?php
require "common.php";
class zwAdd{
    private $common="";
    public function __construct()
    {
        $this->common=new Common();
    }
    public function updateOne($obj){

        if (!empty($obj["id"])){
            if (!empty($obj["zwbt"])){
                $obj["zwnr"]=$this->common->sqlSafeString($obj["zwnr"]);
                $sql="UPDATE `lunwen_yuanfile` SET `正文标题`='".$obj["zwbt"]."',`正文内容`='".$obj["zwnr"]."' WHERE (`id`='".$obj["id"]."')";
                $query=mysqli_query($this->common->startMysql(),$sql);
                if ($query) Common::rs("修改成功");
                Common::rf("写入失败");
            }else{
                Common::rf("标题不得为空");
            }
        }else{
            Common::rf("内容id不存在");
        }

    }
    public function addOne($obj){
        if (!empty($obj["d1"])&&!empty($obj["zwbt"])){
            $obj["zwnr"]=$this->common->sqlSafeString($obj["zwnr"]);
            $obj["d1"]=$this->common->sqlSafeString($obj["d1"]);
            $sql="SELECT * FROM lunwen_yuanfile WHERE user_info_id=".$_SESSION['id']." and d1='".$obj["d1"]."' and is_del=0";
            $query=mysqli_query($this->common->startMysql(),$sql);
            $is_exist=mysqli_fetch_array($query,MYSQLI_ASSOC);
            if ($is_exist) Common::rf("该章已存在");
            $sql="INSERT INTO `lunwen_yuanfile` (`user_info_id`, `stuid`, `d0`, `d1`, `d2`, `d3`, `blokType`, `区块类型`, `正文标题`, `正文内容`)
 VALUES (".$_SESSION['id'].", '".$_SESSION['stuid']."', 1, ".$obj["d1"].", 0, 0, 'mainText', '正文', '".$obj["zwbt"]."', '".$obj["zwnr"]."');";
            $query=mysqli_query($this->common->startMysql(),$sql);
            if ($query) {
                Common::rs("写入成功");
            }else{
                Common::rf("写入失败");
            }

        }else{
            Common::rf("章数或标题不得为空");
        }
    }
    public function addTwo($obj){
        if (empty($obj["d"]))  Common::rf("d1缺失");
        if (!empty($obj["d2"])&&!empty($obj["zwbt"])){
            $obj["zwnr"]=$this->common->sqlSafeString($obj["zwnr"]);
            $obj["d"]=$this->common->sqlSafeString($obj["d"]);
            $obj["d2"]=$this->common->sqlSafeString($obj["d2"]);
            $sql="SELECT * FROM lunwen_yuanfile WHERE user_info_id=".$_SESSION['id']." and d0=2 and d1='".$obj["d"]."' and is_del=0 and d2='".$obj["d2"]."';";

            $query=mysqli_query($this->common->startMysql(),$sql);
            $is_exist=mysqli_fetch_array($query,MYSQLI_ASSOC);
            if ($is_exist) Common::rf("该章已存在");
            $sql="INSERT INTO `lunwen_yuanfile` (`user_info_id`, `stuid`, `d0`, `d1`, `d2`, `d3`, `blokType`, `区块类型`, `正文标题`, `正文内容`)
 VALUES (".$_SESSION['id'].", '".$_SESSION['stuid']."', 2, ".$obj["d"].", ".$obj["d2"].", 0, 'mainText', '正文', '".$obj["zwbt"]."', '".$obj["zwnr"]."');";

            $query=mysqli_query($this->common->startMysql(),$sql);
            if ($query) {
                Common::rs("写入成功");
            }else{
                Common::rf("写入失败");
            }
        }else{
            Common::rf("章数或标题不得为空");
        }
    }

    public function addThree($obj){
        if (empty($obj["d"])) Common::rf("d缺失");
        if (empty($obj["d3"])) Common::rf("章数缺失");
        if (empty($obj["zwbt"])) Common::rf("标题不为空");
        $d_arr=explode("_",$obj["d"]);
        if (empty($d_arr[0])||empty($d_arr[1])) Common::rf("d_arr缺失");
        $where=[];

        $where["d1"]=$this->common->sqlSafeString($d_arr[0]);
        $where["d2"]=$this->common->sqlSafeString($d_arr[1]);
        $where["d3"]=$this->common->sqlSafeString($obj["d3"]);
        $sql="SELECT * FROM lunwen_yuanfile 
WHERE user_info_id=".$_SESSION['id']." and d0=3 and d1='".$where["d1"]."' and is_del=0 and d2='".$where["d2"]."' and d3='".$where["d3"]."'";

        $query=mysqli_query($this->common->startMysql(),$sql);
        $is_exist=mysqli_fetch_array($query,MYSQLI_ASSOC);
        if ($is_exist) Common::rf("该章已存在");
        $insert["zwnr"]=$this->common->sqlSafeString($obj["zwnr"]);
        $insert["zwbt"]=$this->common->sqlSafeString($obj["zwbt"]);
        $sql="INSERT INTO `lunwen_yuanfile` (`user_info_id`, `stuid`, `d0`, `d1`, `d2`, `d3`, `blokType`, `区块类型`, `正文标题`, `正文内容`)
 VALUES (".$_SESSION['id'].", '".$_SESSION['stuid']."', 3, ".$where["d1"].", ".$where["d2"].",".$where["d3"].", 'mainText', '正文', '".$insert["zwbt"]."', '".$insert["zwnr"]."');";

        $query=mysqli_query($this->common->startMysql(),$sql);
        if ($query) {
            Common::rs("写入成功");
        }else{
            Common::rf("写入失败");
        }
    }


}

if (!empty($_POST['type'])){
    $type=$_POST['type'];
    switch ($type){
        case "u1": $class=new zwAdd(); $class->updateOne($_POST);break;
        case "a1": $class=new zwAdd(); $class->addOne($_POST);break;
        case "a2": $class=new zwAdd(); $class->addTwo($_POST);break;
        case "a3": $class=new zwAdd(); $class->addThree($_POST);break;
        default:Common::rf("参数异常") ;
    }

}